<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/17
 * Time: 11:33
 */

namespace app\common\controller;


use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use think\Db;
use think\Request;
use app\index\controller\ReturnData;

class Helptype
{
    /**
     * @param $data
     * 手动设置日志
     */
     public static function insert_log($data){
         $years = date('Y-m');
         //设置路径目录信息
         $url  =  $_SERVER['DOCUMENT_ROOT'].'/'.$years.'/'.date('Ymd').'_money_log.txt';
         $dir_name=dirname($url);
         //目录不存在就创建
         if(!file_exists($dir_name))
         {
             //iconv防止中文名乱码
             $res = mkdir(iconv("UTF-8", "GBK", $dir_name),0777,true);
         }
         $fp = fopen($url,"a");//打开文件资源通道 不存在则自动创建
         fwrite($fp,var_export($data,true)."\r\n");//写入文件
         fclose($fp);//关闭资源通道

     }

    
    /**
     * @param $user_id
     * @return bool
     * 判断的当前用户是否为商家
     */
    public static function is_business($user_id){

        $data = Db::table('users')
             ->where(['id'=>$user_id,'is_business'=>1])
             ->find();
        if(empty($data)){
            return false;
        }
        return true;
    }

    /**
     * @param $array
     * @return array
     * 对象转数组
     */
    public static function object_array($array) {
        if(is_object($array)) {
            $array = (array)$array;
        } if(is_array($array)) {
            foreach($array as $key=>$value) {
                $array[$key] = self::object_array($value);
            }
        }
        return $array;
    }

    public static function search_keyword($data,$user_id){
        $back = ['code'=>666,'msg'=>'ok','data'=>[]];
        try{
            $source = 2;
            if($user_id){
                $source = 1;
            }
            $request = Request::instance();
            Db::table('search_keyword')
                ->insert([
                   'keyword'=>$data,
                    'type'  =>1,
                    'plateform'=>1,
                    'user_id'=>$user_id,
                    'source' =>$source,
                    'ip_add'=>$request->ip(),
                    'create_time'=>date('Y:m:d H:i:s',time())
                ]);

            return $back;
         }catch(Exception$e){
              $back['code'] = -1;
              $back['msg'] = $e->getMessage();
               return $back;
         }
    }

    /**
     * @描述：存储调用易宝接口的日志在数据表
     * @param (int)$type, (string)$name, (string)$request, (string)$response
     * @return array
     * @作者：郑明军
     * @日期：2018-10-16
    **/

    public static function yibaoLog($type, $name, $request, $response)
    {
        $inserData = [
            'type'          => $type,
            'name'          => $name,
            'request'       => $request,
            'response'      => $response,
            'create_time'   => date('Y-m-d H:i:s')
        ];
        $insertLog = Db::table('yibao_log')->insert($inserData);
        if($insertLog){
            return 'ok';
        }else{
            return 'error';
        }

    }

}